//
//  ClassRoomViewController.m
//  SheShiDu
//
//  Created by 单剑秋 on 16/6/11.
//  Copyright © 2016年 单剑秋. All rights reserved.
//

#import "ClassRoomViewController.h"
#import "JQButton.h"
#import "JQFlowayout.h"
#import "FatCollectionViewCell.h"
#import "ChoosCollectionViewCell.h"

@interface ClassRoomViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, QLayoutItemHeightDelegate>

@property (nonatomic, strong) JQButton *firButtonl;
@property (nonatomic, strong) JQButton *secButton;
@property (nonatomic, strong) JQButton *thirButton;
@property (nonatomic, strong) JQButton *fourButton;
@property (nonatomic, strong) NSMutableArray *buttonArr;
@property (nonatomic, strong) NSMutableArray *imageArr;
@property (nonatomic, strong) NSMutableArray *buttonTitleArr;
@property (nonatomic, strong) NSMutableArray *chooseTitleArr;
@property (nonatomic, strong) UICollectionView *holeCollectionView;
@property (nonatomic, strong) UICollectionView *choosCollectionView;

@end

@implementation ClassRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
   
    self.view.backgroundColor = [UIColor whiteColor];
    [self setSegment];
    [self setTagButton];
    [self setCollectionView];
    
}

- (void)setSegment
{
    UISegmentedControl *seg =[[UISegmentedControl alloc] initWithItems:@[@"全屋", @"单图"]];
    seg.frame = CGRectMake(0, 0, SCREEN_WIDTH * (164 / 375.0), SCREEN_HEIGHT * (35 / 667.0));
    seg.selectedSegmentIndex = 0;
    seg.backgroundColor = [UIColor whiteColor];
    seg.tintColor = [UIColor colorWithRed:28 / 255.0 green:186 / 255.0 blue:173 / 255.0 alpha:1];
    [seg addTarget:self action:@selector(segAction) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = seg;
}

- (void)segAction
{
    [self.holeCollectionView reloadData];
}

- (void)setTagButton
{
    NSArray *titleArr = [NSArray arrayWithObjects: @"空间", @"风格", @"户型", @"颜色", nil];
    self.firButtonl = [JQButton buttonWithType:UIButtonTypeCustom];
    self.secButton = [JQButton buttonWithType:UIButtonTypeCustom];
    self.thirButton = [JQButton buttonWithType:UIButtonTypeCustom];
    self.fourButton = [JQButton buttonWithType:UIButtonTypeCustom];
    self.buttonArr = [NSMutableArray arrayWithObjects:self.firButtonl, self.secButton, self.thirButton, self.fourButton, nil];
    int k = 0;
    for (JQButton *sonButton in _buttonArr) {
        
        
        sonButton.frame = CGRectMake( k * self.view.frame.size.width / 4, 64, self.view.frame.size.width / 4, self.view.frame.size.height / 17);
        [sonButton setTitle:[titleArr objectAtIndex:k] forState:UIControlStateNormal];
        [sonButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:sonButton];
        k++;
    }
    
}

- (void)buttonAction:(JQButton *)btn
{
    NSArray *firArr = [NSArray arrayWithObjects: @"玄关", @"卧室", @"书房", @"客厅", @"厨房", nil];
    NSArray *secArr = [NSArray arrayWithObjects: @"地中海", @"美式", @"古典", @"日式", @"现代", nil];
    NSArray *thirArr = [NSArray arrayWithObjects: @"一居", @"二居", @"三居", @"复式", @"别墅", nil];
    NSArray *fourArr = [NSArray arrayWithObjects: @"深蓝", @"暖色", @"冷色", @"绿色", @"白色", nil];
    self.buttonTitleArr = [NSMutableArray arrayWithObjects: firArr, secArr, thirArr, fourArr, nil];
    [btn buttonTypeChange];
    [self.choosCollectionView removeFromSuperview];
    if (btn.selected) {
        for (JQButton *sonButton in _buttonArr) {
            if (sonButton != btn) {
                sonButton.backgroundColor = [UIColor whiteColor];
                sonButton.selected = NO;
            } else {
                 btn.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:234 / 255.0 blue:234 / 255.0 alpha:1];
            }
        }
       
        [self ChoosCollectionView:self.choosCollectionView];
    } else {
        btn.backgroundColor = [UIColor whiteColor];
    }
    
}

- (void)ChoosCollectionView:(UICollectionView *)chooseCollectionView;
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(80, 40);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.choosCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + self.view.frame.size.height / 17, self.view.frame.size.width, 130) collectionViewLayout:flowLayout];
    
    _choosCollectionView.dataSource = self;
    _choosCollectionView.delegate = self;
    _choosCollectionView.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:234 / 255.0 blue:234 / 255.0 alpha:1];
    [_choosCollectionView registerClass:[ChoosCollectionViewCell class] forCellWithReuseIdentifier:@"Choose"];
    [self.view insertSubview:_choosCollectionView aboveSubview:_holeCollectionView];
}

- (void)setCollectionView {
    self.imageArr = [NSMutableArray array];
    for (int l = 0; l < 12; l++) {
        NSString *str = [NSString stringWithFormat:@"tu%d",l];
        [_imageArr addObject:[UIImage imageNamed:str]];
    }
    
    JQFlowayout *layout = [[JQFlowayout alloc] init];
    layout.columCounts = 2;
    layout.columSpace = 10;
    layout.rowSpace = 10;
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    
    layout.delegate = self;
    
    self.holeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + self.view.frame.size.height / 17, self.view.frame.size.width, self.view.frame.size.height - 64 - self.view.frame.size.height / 17 - 49) collectionViewLayout:layout];
    _holeCollectionView.dataSource = self;
    _holeCollectionView.delegate = self;
    _holeCollectionView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_holeCollectionView];
    
    [_holeCollectionView registerClass:[FatCollectionViewCell class] forCellWithReuseIdentifier:@"Fat"];
    
//    __weak ClassRoomViewController *vc = self;
    
}

- (CGFloat)layout:(JQFlowayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width
{
    return arc4random() % 40 + 120;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _holeCollectionView) {
        return _imageArr.count;
    } else {
        return _buttonTitleArr.count + 1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _holeCollectionView) {
        FatCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Fat" forIndexPath:indexPath];
        cell.mainImageView.image = [_imageArr objectAtIndex:indexPath.item];
        return cell;
    } else {
        ChoosCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Choose" forIndexPath:indexPath];
//        cell.titleLabel.text = [_buttonTitleArr objectAtIndex:indexPath.item];
        int x = 0;
        for (JQButton *sonButton in _buttonArr) {
            if (sonButton.selected) {
             cell.titleLabel.text = [[_buttonTitleArr objectAtIndex:x] objectAtIndex:indexPath.item];
            }
            x++;
        }
        return cell;

    }
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _choosCollectionView) {
        int x = 0;
        for (JQButton *sonButton in _buttonArr) {
            if (sonButton.selected) {
                [sonButton setTitle:[[_buttonTitleArr objectAtIndex:x] objectAtIndex:indexPath.item] forState:UIControlStateNormal];
                [sonButton buttonTypeChange];
                sonButton.backgroundColor = [UIColor whiteColor];
            }
            x++;
        }
        [_choosCollectionView removeFromSuperview];
        [_holeCollectionView reloadData];
        [_holeCollectionView setContentOffset:CGPointMake(0, 0)];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
